﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class CreateExcel_CreateSmsStatisticsExcel : System.Web.UI.Page
{
    public static string Label1 = "0";
    public static string Label2 = "0";
    public static string Label3 = "0";
    public static string Label4 = "0";
    public static string Label5 = "0";
    public static string Label6 = "0";
    public static string Label7 = "0";
    public static string Label8 = "0";
    public static string Label9 = "0";
    public static string Label10 = "0";
    public static string Label11 = "0";
    public static string Label12 = "0";
    public static string Label13 = "0";
    public static string Label14 = "0";

    public static string name = string.Empty;

    public static string[] statusList = new string[4] { "0", "0", "0", "0"};
    public static string[] typeList = new string[9] { "0", "0", "0", "0","0", "0", "0", "0","0" };

    protected void Page_Load(object sender, EventArgs e)
    {
        string startTime = "";
        string endTime = "";
        string status = "";
        string types = "";

        if (Request.QueryString["name"] != null && Request.QueryString["name"].ToString() != "")
        {
            name = Request.QueryString["name"].ToString();
        }
        if (Request.QueryString["startTime"] != null && Request.QueryString["startTime"].ToString() != "")
        {
            startTime = Request.QueryString["startTime"].ToString();
        }
        if (Request.QueryString["endTime"] != null && Request.QueryString["endTime"].ToString() != "")
        {
            endTime = Request.QueryString["endTime"].ToString();
        }
        if (Request.QueryString["status"] != null && Request.QueryString["status"].ToString() != "")
        {
            status = Request.QueryString["status"].ToString();
        }
        if (Request.QueryString["type"] != null && Request.QueryString["type"].ToString() != "")
        {
            types = Request.QueryString["type"].ToString();
        }

        Bind(startTime, endTime, status, types);

        gvData.DataSource = CreateDataTable();
        gvData.DataBind();

        ShowExcel();

    }

    #region 导出Excel
    public void ShowExcel()
    {
        Response.Clear();
        Response.Buffer = false;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(Server.HtmlDecode(name), System.Text.Encoding.UTF8) + ".xls");

        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-excel";
        Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
        gvData.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
    }
    #endregion

    #region 绑定数据
    private void Bind(string st,string et,string status,string type)
    {
        statusList[0] = new Cisoft.Cisoft3G.BLL.DraftsBO().GetList("").Tables[0].Rows.Count.ToString();
        statusList[1] = new Cisoft.Cisoft3G.BLL.InboxBO().GetList(" IB_SendTime >= '" + st + "' and IB_SendTime <= '" + et + "' ").Tables[0].Rows.Count.ToString();
        statusList[2] = new Cisoft.Cisoft3G.BLL.OutboxBO().GetList("").Tables[0].Rows.Count.ToString();
        statusList[3] = new Cisoft.Cisoft3G.BLL.SentBO().GetList(" ST_SentTime >= '" + st + "' and ST_SentTime <= '" + et + "' ").Tables[0].Rows.Count.ToString();


        if (status == "0")
        {
            if (type == "0")
            {
                for (int j = 5; j < 14; j++)
                {
                    int i = new Cisoft.Cisoft3G.BLL.SentBO().GetList(" ST_SentTime >= '" + st + "' and ST_SentTime <= '" + et + "' and ST_SmsType ='" + new Cisoft.Cisoft3G.BLL.SmsTypeBO().GetModel(j-4).ST_TypeName + "' ").Tables[0].Rows.Count +
                            new Cisoft.Cisoft3G.BLL.OutboxBO().GetList(" OB_SendTime >= '" + st + "' and OB_SendTime <= '" + et + "' and RT_Type_FK =" + (j - 4).ToString()).Tables[0].Rows.Count +
                            new Cisoft.Cisoft3G.BLL.InboxBO().GetList(" IB_SendTime >= '" + st + "' and IB_SendTime <= '" + et + "' and RT_Type_FK =" + (j - 4).ToString()).Tables[0].Rows.Count;
                    typeList[j-5] = i.ToString();
                }
            }
            if (type != "0")
            {
                for (int i = 1; i < 10; i++)
                {

                    int j =  (new Cisoft.Cisoft3G.BLL.InboxBO().GetList(" IB_SendTime >= '" + st + "' and IB_SendTime <= '" + et + "' and RT_Type_FK =" + i.ToString() + "").Tables[0].Rows.Count +
                                    new Cisoft.Cisoft3G.BLL.OutboxBO().GetList(" OB_SendTime >= '" + st + "' and OB_SendTime <= '" + et + "' and RT_Type_FK =" + i.ToString() + "").Tables[0].Rows.Count +
                                    new Cisoft.Cisoft3G.BLL.SentBO().GetList(" ST_SentTime >= '" + st + "' and ST_SentTime <= '" + et + "' and ST_SmsType ='" + new Cisoft.Cisoft3G.BLL.SmsTypeBO().GetModel(i).ST_TypeName + "' ").Tables[0].Rows.Count);
                    typeList[i-1] = j.ToString();
                }
            }
        }
        else
        {
            if (type == "0")
            {
                for (int i = 1; i < 5; i++)
                {
                    switch (status)
                    {
                        case "CG_Drafts":
                            for (int k = 1; k < 10; k++)
                            {
                                 typeList[i-1] = new Cisoft.Cisoft3G.BLL.DraftsBO().GetAllList().Tables[0].Rows.Count.ToString();
                            }
                            break;
                        case "CG_Inbox":
                            for (int k = 1; k < 10; k++)
                            {
                                typeList[i-1]= new Cisoft.Cisoft3G.BLL.InboxBO().GetList(" IB_SendTime >= '" + st + "' and IB_SendTime <= '" + et + "' and RT_Type_FK =" + k.ToString()).Tables[0].Rows.Count.ToString();
                            }
                            break;
                        case "CG_Outbox":
                            for (int k = 1; k < 10; k++)
                            {
                                typeList[i-1] = new Cisoft.Cisoft3G.BLL.OutboxBO().GetList(" OB_SendTime >= '" + st + "' and OB_SendTime <= '" + et + "' and RT_Type_FK =" + k.ToString()).Tables[0].Rows.Count.ToString();
                            }
                            break;
                        case "CG_Sent":
                            for (int k = 1; k < 10; k++)
                            {
                                typeList[i - 1] = new Cisoft.Cisoft3G.BLL.SentBO().GetList(" ST_SentTime >= '" + st + "' and ST_SentTime <= '" + et + "' and ST_SmsType ='" + new Cisoft.Cisoft3G.BLL.SmsTypeBO().GetModel(k).ST_TypeName  + "' ").Tables[0].Rows.Count.ToString();
                            }
                            break;
                    }
                }
            }
            if (type != "0")
            {
                switch (status)
                {
                    case "CG_Drafts":
                        for (int k = 1; k < 10; k++)
                        {
                            typeList[k-1] = new Cisoft.Cisoft3G.BLL.DraftsBO().GetAllList().Tables[0].Rows.Count.ToString();
                        }
                        break;
                    case "CG_Inbox":
                        for (int k = 1; k < 10; k++)
                        {
                            typeList[k-1] = new Cisoft.Cisoft3G.BLL.InboxBO().GetList(" IB_SendTime >= '" + st + "' and IB_SendTime <= '" + et + "' and RT_Type_FK =" + k.ToString()).Tables[0].Rows.Count.ToString();
                        }
                        break;
                    case "CG_Outbox":
                        for (int k = 1; k < 10; k++)
                        {
                            typeList[k-1] = new Cisoft.Cisoft3G.BLL.OutboxBO().GetList(" OB_SendTime >= '" + st + "' and OB_SendTime <= '" + et + "' and RT_Type_FK =" + k.ToString()).Tables[0].Rows.Count.ToString();

                        }
                        break;
                    case "CG_Sent":
                        for (int k = 1; k < 10; k++)
                        {
                            typeList[k - 1] = new Cisoft.Cisoft3G.BLL.SentBO().GetList(" ST_SentTime >= '" + st + "' and ST_SentTime <= '" + et + "' and ST_SmsType ='" + new Cisoft.Cisoft3G.BLL.SmsTypeBO().GetModel(k).ST_TypeName + "' ").Tables[0].Rows.Count.ToString();
                        }
                        break;
                }
            }
        }
    }
    #endregion

    #region 自定义DataTable
    private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();

        DataColumn dc1 = new DataColumn("短信接收条数");
        DataColumn dc2 = new DataColumn("短信发送条数");
        DataColumn dc3 = new DataColumn("待发短信条数");
        DataColumn dc4 = new DataColumn("草稿箱草稿数量");
        DataColumn dc5 = new DataColumn("普通");
        DataColumn dc6 = new DataColumn("通知");
        DataColumn dc7 = new DataColumn("公告");
        DataColumn dc8 = new DataColumn("投票");
        DataColumn dc9 = new DataColumn("抽奖");
        DataColumn dc10 = new DataColumn("意见");
        DataColumn dc11 = new DataColumn("投诉");
        DataColumn dc12 = new DataColumn("提醒");
        DataColumn dc13 = new DataColumn("答题");

        dt.Columns.Add(dc1);
        dt.Columns.Add(dc2);
        dt.Columns.Add(dc3);
        dt.Columns.Add(dc4);
        dt.Columns.Add(dc5);
        dt.Columns.Add(dc6);
        dt.Columns.Add(dc7);
        dt.Columns.Add(dc8);
        dt.Columns.Add(dc9);
        dt.Columns.Add(dc10);
        dt.Columns.Add(dc11);
        dt.Columns.Add(dc12);
        dt.Columns.Add(dc13);

        DataRow row = dt.NewRow();
        object[] obs = new object[13]{statusList[0],statusList[1],statusList[2],statusList[3],typeList[0],
                   typeList[1],typeList[2],typeList[3],typeList[4],typeList[5],typeList[6],typeList[7],typeList[8]};
        row.ItemArray = obs;
        dt.Rows.Add(row);
        return dt;
    }
    #endregion

    public override void VerifyRenderingInServerForm(Control control)
    {
    }
}
